% author: ziyan (zoe) zhu
% email: zzhu1@g.harvard.edu
function Am_dom = calc_moire_dom_tri(q12, q23, A0)
    grid_search = 1; 

    A12 = moireh_calc(A0, q12, 0, 1, 1);
    A23 = moireh_calc(A0, q23, 0, 1, 1);
    disp(A12)
    
    if q12*q23 > 0 
        rot180 = [cos(pi), -sin(pi); 
                  sin(pi), cos(pi)];
        A12 = rot180*A12;
    end 
    
    G12 = 2*pi*transpose(inv(A12));
    G23 = 2*pi*transpose(inv(A23));

    a12 = -A12(:,1);
    a23 = A23(:,1);

    g12 = -G12(:,1);
    g23 = G23(:,1);

    % bilayer moire length
    ml12 = norm(a12);
    ml23 = norm(a23);

    % angle between the bilayer moires 
    theta = acos(dot(a12, a23)/(ml12*ml23));
    disp(theta)

    % find the dominant harmonic 
    for i1 = 1:grid_search 
        for i2 = 1:grid_search 

            g_vec = i1*g12 - i2*g23; 
            g_vec_norm(i1,i2) = norm(g_vec);
            
      
        end 
    end 

    gvecnorm_list = sort(g_vec_norm);

    [m_dom,n_dom] = find(g_vec_norm==min(g_vec_norm(:)));
    if length(m_dom) > 1 
        [m_dom, a] = min(m_dom);
        n_dom = n_dom(a);
    end 
    
%     disp('---------------')
%     disp([m_dom, n_dom]);

    delta = ml12/ml23*m_dom/n_dom-1;
    Am_dom = moireh_calc(-A12, theta, ml23/ml12-1, m_dom, n_dom);
end 
